import 'dart:async';

import 'package:flutter/material.dart';
import 'package:net_music/root_page.dart';
import 'package:net_music/util/screen_util.dart';

class LaunchPage extends StatefulWidget {
  const LaunchPage({Key? key}) : super(key: key);

  @override
  _LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
  int _currentTime = 3;
  late Timer _timer;
  @override
  void initState() {
    super.initState();

    _timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
      setState(() {
        _currentTime--;
      });
      if (_currentTime <= 0) {
        _jump2RootPage();
      }
    });
  }

  void _jump2RootPage() {
    _timer.cancel();

    print('safe-bottom-height ${Screen.safeBottomHeight(context)}');
    print('status-bar-height ${Screen.statusBarHeight(context)}');
    Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(builder: (BuildContext context) => RootPage()),
        (route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        Image.asset(
          'assets/images/common/page.png',
          fit: BoxFit.cover,
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
        ),
        Positioned(
          child: InkWell(
            child: _clipButton(),
            onTap: _jump2RootPage,
          ),
          right: 10,
          top: MediaQuery.of(context).padding.top,
        )
      ],
    ));
  }

  Widget _clipButton() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(50),
      child: Container(
        alignment: Alignment.center,
        width: 50,
        height: 40,
        color: Colors.black.withOpacity(0.5),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              "跳过",
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  decoration: TextDecoration.none),
            ),
            Text(
              '${_currentTime}s',
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  decoration: TextDecoration.none),
            )
          ],
        ),
      ),
    );
  }
}
